#include "blockQueue.hpp"

using namespace std;

void *custom(void *args)
{
    BlockQueue<int> *bq = static_cast<BlockQueue<int> *>(args);

    while (true)
    {
        sleep(1);
        int data = 0;
        bq->pop(&data);
        cout << "custom data" << data << endl;
    }
}

void *product(void *args)
{
    BlockQueue<int> *bq = static_cast<BlockQueue<int> *>(args);
    while (true)
    {
        sleep(1);
        int data = rand() % 10 + 1;
        bq->push(data);
        cout << "product data" << data << endl;
    }
}

void single_test()
//单生产单消费，整型数据
{
    srand((uint64_t)time(nullptr) ^ getpid());
    pthread_t c, p;
    BlockQueue<int> *bq = new BlockQueue<int>();
    pthread_create(&c, nullptr, custom, bq);
    pthread_create(&p, nullptr, product, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete bq;
}

void many_test()
//多生成多消费
{
    pthread_t c[5], p[5];
    BlockQueue<int> *bq = new BlockQueue<int>();
    pthread_create(&c[1], nullptr, custom, bq);
    pthread_create(&c[2], nullptr, custom, bq);
    pthread_create(&c[3], nullptr, custom, bq);
    pthread_create(&c[4], nullptr, custom, bq);

    pthread_create(&p[1], nullptr, product, bq);
    pthread_create(&p[2], nullptr, product, bq);
    pthread_create(&p[3], nullptr, product, bq);
    pthread_create(&p[4], nullptr, product, bq);


    pthread_join(c[1], nullptr);
    pthread_join(c[2], nullptr);
    pthread_join(c[3], nullptr);
    pthread_join(c[4], nullptr);
    pthread_join(p[1], nullptr);
    pthread_join(p[2], nullptr);
    pthread_join(p[3], nullptr);
    pthread_join(p[4], nullptr);
}

int main()
{
    single_test();
    //many_test();

    return 0;
}